{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial for analyzing instrumental learning data with the HDDMnnRL module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hddm\n",
    "import pickle\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# with open('./rlssm_data.pickle', 'rb') as handle:\n",
    "#     datafile = pickle.load(handle)\n",
    "# print(len(datafile))\n",
    "\n",
    "# dataset = datafile[0]\n",
    "# data = hddm.utils.get_dataset_as_dataframe_rlssm(dataset)\n",
    "# data.to_csv('rlssm_data.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Load the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the data\n",
    "data = pd.read_csv(\"rlssm_data.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Initialize the HDDMnnRL model and sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Specify number of samples and burnins\n",
    "nsamples = 100\n",
    "nburn = 50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = hddm.HDDMnnRL(\n",
    "    data,\n",
    "    model=\"angle\",\n",
    "    rl_rule=\"RWupdate\",\n",
    "    non_centered=True,\n",
    "    include=[\"v\", \"a\", \"t\", \"z\", \"theta\", \"rl_alpha\"],\n",
    "    p_outlier=0.0,\n",
    ")\n",
    "m.sample(nsamples, burn=nburn, dbname=\"traces.db\", db=\"pickle\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Save the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save the model\n",
    "m.save(\"rlssm_model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the model\n",
    "model = hddm.load(\"rlssm_model\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Check the posterior results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.plot_posteriors()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the trace\n",
    "with open(\"./traces.db\", \"rb\") as handle:\n",
    "    tracefile = pickle.load(handle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Re-format traces as a dataframe\n",
    "traces = hddm.utils.get_traces_rlssm(tracefile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_ssm = \"angle\"\n",
    "model_rl = \"RWupdate\"\n",
    "\n",
    "config_ssm = hddm.model_config.model_config[model_ssm]\n",
    "config_rl = hddm.model_config_rl.model_config_rl[model_rl]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = hddm.plotting.plot_posterior_pairs_rlssm(\n",
    "    tracefile, config_ssm[\"params\"] + config_rl[\"params\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Posterior Predictive Checks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_posterior_samples = 3\n",
    "p_lower = {0: 0.15, 1: 0.30, 2: 0.45}\n",
    "p_upper = {0: 0.85, 1: 0.70, 2: 0.55}\n",
    "ppc_sdata = hddm.plotting.gen_ppc_rlssm(\n",
    "    model_ssm,\n",
    "    config_ssm,\n",
    "    model_rl,\n",
    "    config_rl,\n",
    "    data,\n",
    "    traces,\n",
    "    num_posterior_samples,\n",
    "    p_lower,\n",
    "    p_upper,\n",
    "    save_data=True,\n",
    "    save_name=\"ppc_data\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the saved ppc data\n",
    "# ppc_sdata = pd.read_csv('./ppc_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = hddm.plotting.plot_ppc_choice_rlssm(data, ppc_sdata, 40, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = hddm.plotting.plot_ppc_rt_rlssm(data, ppc_sdata, 40, 0.06)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "42954fe55a2f14013fd58fa84e2b5ba0a82ff2e02017ed2b35460e7843d4b63d"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 ('hddm_py38')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
